package com.yswy.common.utils;

import java.util.AbstractList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

public class ListUtils {

    public static <T> List<List<T>> partition(final List<T> list, final int size) {
        if (list == null) {
            throw new NullPointerException("List must not be null");
        }
        if (size <= 0) {
            throw new IllegalArgumentException("Size must be greater than 0");
        }
        return new Partition<>(list, size);
    }

    public static List<Integer> StringToList(String listStr){
        listStr = listStr.substring(1);
        listStr = listStr.substring(0 ,listStr.length()-1);
        List<Integer> list = Arrays.stream(listStr.split(",")).map(s->Integer.parseInt(s.trim())).collect(Collectors.toList());
        return list ;
    }

        private static class Partition<T> extends AbstractList<List<T>> {
            private final List<T> list;
            private final int size;

            private Partition(final List<T> list, final int size) {
                this.list = list;
                this.size = size;
            }

            @Override
            public List<T> get(final int index) {
                final int listSize = size();
                if (index < 0) {
                    throw new IndexOutOfBoundsException("Index " + index + " must not be negative");
                }
                if (index >= listSize) {
                    throw new IndexOutOfBoundsException("Index " + index + " must be less than size " +
                            listSize);
                }
                final int start = index * size;
                final int end = Math.min(start + size, list.size());
                return list.subList(start, end);
            }

            @Override
            public int size() {
                return (int) Math.ceil((double) list.size() / (double) size);
            }

            @Override
            public boolean isEmpty() {
                return list.isEmpty();
            }
        }



    /**
     * 判断List不为空,非空返回true,空则返回false
     *
     * @param list
     * @return boolean
     */
    public static boolean isNotNull(List<?> list) {

        if (null != list) {
            if ((list.size() > 0) && !list.isEmpty()) {
                return true;
            }
        }
        return false;
    }

    /**
     * 判断List是为空,为空返回true,非空则返回false
     *
     * @param list
     * @return boolean
     */
    public static boolean isNull(List<?> list) {

        if (null == list || list.size() == 0 || list.isEmpty()) {
            return true;
        }
        return false;
    }
    }